﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InfFront.InfoViews.Base;
using System.Windows.Media.Imaging;
using System.Windows.Controls;
using Microsoft.Windows.Controls;
using System.Windows;
using System.Windows.Data;
using InfFront.UICore;

namespace InfFront.InfoViews.WPF
{
    public static class Imaging
    {
        public static BitmapSource GetIcon(this IIconedObject source, int hSize, int vSize, bool disabled, bool highlighted)
        {
            var stream = source.GetIconStreamPng(hSize, vSize, disabled, highlighted);
            if (stream == null)
                return null;
            var decoder = new PngBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.Default);
            BitmapSource result = decoder.Frames[0];
            return result;
        }
    }

    public class IconedObjectToImageConverter : System.Windows.Data.IValueConverter
    {
        private int size;

        public IconedObjectToImageConverter(int size)
        {
            this.size = size;
        }

        public object Convert(object value, Type targetType, object parameter,
                              System.Globalization.CultureInfo culture)
        {
            var src = value as IIconedObject;
            if (src != null)
            {
                return src.GetIcon(size, size, false, false);
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter,
                                  System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

    public class IconedObjectToNormal24x24ImageConverter : IconedObjectToImageConverter
    {
        public IconedObjectToNormal24x24ImageConverter()
            : base(24)
        { }
    }

    public class IconedObjectToNormal48x48ImageConverter : IconedObjectToImageConverter
    {
        public IconedObjectToNormal48x48ImageConverter()
            : base(48)
        { }
    }

    public class IconedObjectToNormal16x16ImageConverter : IconedObjectToImageConverter
    {
        public IconedObjectToNormal16x16ImageConverter()
            : base(16)
        { }
    }

    public class UICoreIImageToImageConverter : System.Windows.Data.IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
                              System.Globalization.CultureInfo culture)
        {
            var src = value as IImage;
            if (src != null)
            {
                var stream = src.GetData();
                if (stream != null)
                {
                    var decoder = new PngBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.Default);
                    BitmapSource result = decoder.Frames[0];
                    return result;
                }
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter,
                                  System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }

    /////////////////////////////////////////////////////////////////////////////////////
    /// <summary>
    /// DataGridImageColumn
    /// </summary>
    public class DataGridImageColumn : DataGridBoundColumn
    {
        // Fields

        // Methods
        static DataGridImageColumn()
        {
        }

        protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
        {
            throw new NotSupportedException();
        }

        protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
        {
            Image image = (cell != null) ? (cell.Content as Image) : null;
            if (image == null)
                image = new Image();
            BindingOperations.SetBinding(image, Image.SourceProperty, Binding);
            return image;
        }
    }

}
